% Basic Introduction:
% This function expands a unit block matrix to a diagonal blockwise
% matrix.
function [varargout]=SpaFun_Block2Diag(M_block,Multiple,OutputFlag)

[D1,D2]         =   size(M_block);
[li,lj,Val]     =   find(M_block);
N               =   length(Val);

temp            =   reshape(repmat((0:1:Multiple-1),[N,1]),...
                            [N*Multiple,1]);
Li              =   repmat(li,[Multiple,1])+temp*D1;
Lj              =   repmat(lj,[Multiple,1])+temp*D2;
if nargin<3
    OutputFlag  =   'Matrix';
end
switch OutputFlag
    case 'Matrix'
        varargout{1}    =   sparse(Li,Lj,repmat(Val,[Multiple,1]),...
                                   D1*Multiple,D2*Multiple);
    case 'Index & Value'
        varargout{1}    =   Li;
        varargout{2}    =   Lj;
        varargout{3}    =   repmat(Val,[Multiple,1]);
end
 

